import random
import json

class RubiksCube:
    def __init__(self, size):
        self.size = size
        self.cube = self.initialize_cube()

    def initialize_cube(self):
        colors = ['R', 'G', 'B', 'Y', 'O', 'W']  # Red, Green, Blue, Yellow, Orange, White
        cube = {}
        for face in ['前', '后', '左', '右', '上', '下']:
            cube[face] = [[random.choice(colors) for _ in range(self.size)] for _ in range(self.size)]
        return cube

    def rotate_face_clockwise(self, face):
        self.cube[face] = [list(row) for row in zip(*reversed(self.cube[face]))]

    def rotate_face_counterclockwise(self, face):
        self.cube[face] = [list(row) for row in reversed(list(zip(*self.cube[face])))]

    def print_cube(self):
        for face, matrix in self.cube.items():
            print(f'{face}:')
            for row in matrix:
                print(row)
            print()

    def play_game(self):
        # Implement game logic here
        pass

    def save_solution(self, steps, filename='solution.json'):
        with open(filename, 'w') as file:
            json.dump(steps, file)

    def upgrade_cube_size(self):
        self.size += 1
        self.cube = self.initialize_cube()


if __name__ == "__main__":
    size = 3  # 初始魔方大小
    cube = RubiksCube(size)
    cube.print_cube()


    cube.rotate_face_clockwise('前')
    cube.rotate_face_counterclockwise('右')
    cube.print_cube()

    # 游戏结束
    steps = [{"rotate": "前", "direction": "clockwise"}, {"rotate": "右", "direction": "counterclockwise"}]
    cube.save_solution(steps)

    # 升级魔方大小
    cube.upgrade_cube_size()
    cube.print_cube()
